home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tfile.cc < prev    next >
C/C++ Source or Header  |  1993-04-27  |  8KB  |  372 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  * a few tests for streams
  5.  *
  6.  */
  7.  
  8. #include <stream.h>
  9. #ifndef _OLD_STREAMS
  10. #include <strstream.h>
  11. #include "unistd.h"
  12. #endif
  13. #include <SFile.h>
  14. #include <PlotFile.h>
  15.  
  16. #include <std.h>
  17. #include <assert.h>
  18.  
  19. class record
  20. {
  21. public:
  22.   char c; int i; double d;
  23. };
  24.  
  25. ostream& operator<<(ostream& s, record& r)
  26. {
  27.   return(s << "(i = " << r.i << " c = " << r.c << " d = " << r.d << ")");
  28. }
  29.  
  30. void t1()
  31. {
  32.   char ch;
  33.  
  34.   assert(cout.good());
  35.   assert(cout.writable());
  36.   assert(cout.is_open());
  37.   cout << "Hello, world via cout\n";
  38.   assert(cerr.good());
  39.   assert(cerr.writable());
  40.   assert(cerr.is_open());
  41.   cerr << "Hello, world via cerr\n";
  42.  
  43.   assert(cin.good());
  44.   assert(cin.readable());
  45.   assert(cin.is_open());
  46.  
  47.   cout << "enter a char:";  cin >> ch;
  48.   cout.put('c');  cout.put(' ');  cout.put('=');  cout.put(' ');
  49.   cout.put('"');  cout.put(ch);    cout << '"';  cout << char('\n');
  50.   assert(cin.good());
  51.   assert(cout.good());
  52. }
  53.  
  54. void t2()
  55. {
  56.   int i;
  57.   short h;
  58.   long l;
  59.   float f;
  60.   double d;
  61.   char s[100];
  62.  
  63.   cout << "enter three integers (short, int, long):";  
  64.   cin >> h; cin >> i;   
  65.   // cin.scan("%ld", &l);
  66.   cin >> l;
  67.   cout << "first  = " << h << " via dec = " << dec(h, 8) << "\n";
  68.   cout << "second = " << i << form(" via form = %d = 0%o", i, i);
  69.   cout.form(" via cout.form = %d = 0x%x\n", i, i);
  70.   cout << "third  = " << l  << " via hex = " << hex(l) << "\n";
  71.   assert(cin.good());
  72.   assert(cout.good());
  73.  
  74.   cout << "enter a float then a double:";  cin >> f; cin >> d;
  75.   cout << "first  = " << f << "\n";
  76.   cout << "second = " << d << "\n";
  77.   assert(cin.good());
  78.   assert(cout.good());
  79.  
  80.   cout << "enter 5 characters separated with spaces:";  cin >> s;
  81.   cout << "first  = " << s << "\n";
  82.   cin.get(s, 100);
  83.   cout << "rest   = " << s << "\n";
  84.  
  85.   assert(cin.good());
  86.   assert(cout.good());
  87.  
  88. }
  89.  
  90. void t3()
  91. {
  92.   char ch;
  93.   cout << "\nMaking streams sout and sin...";
  94. #ifdef _OLD_STREAMS
  95.   ostream sout("streamfile", io_writeonly, a_create);
  96. #else
  97.   ofstream sout("streamfile");
  98. #endif
  99.   assert(sout.good());
  100.   assert(sout.is_open());
  101.   assert(sout.writable());
  102.   assert(!sout.readable());
  103.   sout << "This file has one line testing output streams.\n";
  104.   sout.close();
  105.   assert(!sout.is_open());
  106. #ifdef _OLD_STREAMS
  107.   istream sin("streamfile", io_readonly, a_useonly);
  108. #else
  109.   ifstream sin("streamfile");
  110. #endif
  111.   assert(sin.good());
  112.   assert(sin.is_open());
  113.   assert(!sin.writable());
  114.   assert(sin.readable());
  115.   cout << "contents of file:\n";
  116.   while(sin >> ch) cout << ch;
  117.   sin.close();
  118.   assert(!sin.is_open());
  119. }
  120.  
  121.  
  122. void t4()
  123. {  
  124.   char s[100];
  125.   char ch;
  126.   int i;
  127.  
  128.   cout << "\nMaking File tf ... "; 
  129. #ifdef _OLD_STREAMS
  130.   File tf("tempfile", io_readwrite, a_create);
  131. #else
  132.   fstream tf("tempfile", ios::in|ios::out|ios::trunc);
  133. #endif
  134.   assert(tf.good());
  135.   assert(tf.is_open());
  136.   assert(tf.writable());
  137.   assert(tf.readable());
  138.   strcpy(s, "This is the first and only line of this file.\n");
  139. #ifdef _OLD_STREAMS
  140.   tf.put(s);
  141.   tf.seek(0);
  142. #else
  143.   tf << s;
  144.   tf.rdbuf()->seekoff(0, ios::beg);
  145. #endif
  146.   tf.get(s, 100);
  147.   assert(tf.good());
  148.   cout << "first line of file:\n" << s << "\n";
  149.   cout << "next char = ";
  150.   tf.get(ch);
  151.   cout << (int)ch;
  152.   cout.put('\n');
  153.   assert(ch == 10);
  154.   strcpy(s, "Now there is a second line.\n");
  155.   cout << "reopening tempfile, appending: " << s;
  156. #ifdef _OLD_STREAMS
  157.   tf.open(tf.name(), io_appendonly, a_use);
  158. #else
  159.   tf.close();
  160.   tf.open("tempfile", ios::app);
  161. #endif
  162.   assert(tf.good());
  163.   assert(tf.is_open());
  164.   assert(tf.writable());
  165.   assert(!tf.readable());
  166. #ifdef _OLD_STREAMS
  167.   tf.put(s);
  168.   assert(tf.good());
  169.   tf.open(tf.name(), io_readonly, a_use);
  170. #else
  171.   tf << s;
  172.   assert(tf.good());
  173.   tf.close();
  174.   tf.open("tempfile", ios::in);
  175. #endif
  176.   tf.raw();
  177.   assert(tf.good());
  178.   assert(tf.is_open());
  179.   assert(!tf.writable());
  180.   assert(tf.readable());
  181.   cout << "First 10 chars via raw system read after reopen for input:\n";
  182.   read(tf.filedesc(), s, 10);
  183.   assert(tf.good());
  184.   for (i = 0; i < 10; ++ i)
  185.     cout.put(s[i]);
  186.   lseek(tf.filedesc(), 5, 0);
  187.   cout << "\nContents after raw lseek to pos 5:\n";
  188.   while ( (tf.get(ch)) && (cout.put(ch)) );
  189. #ifdef _OLD_STREAMS
  190.   tf.remove();
  191. #else
  192.   tf.close();
  193.   unlink("tempfile");
  194. #endif
  195.   assert(!tf.is_open());
  196. }
  197.  
  198. void t5()
  199. {
  200.   record r;
  201.   int i;
  202.   cout << "\nMaking SFile rf...";
  203. #ifdef _OLD_STREAMS
  204.   SFile rf("recfile", sizeof(record), io_readwrite, a_create);
  205. #else
  206.   SFile rf("recfile", sizeof(record), ios::in|ios::out|ios::trunc);
  207. #endif
  208.   assert(rf.good());
  209.   assert(rf.is_open());
  210.   assert(rf.writable());
  211.   assert(rf.readable());
  212.   for (i = 0; i < 10; ++i)
  213.   {
  214.     r.c = i + 'a';
  215.     r.i = i;
  216.     r.d = (double)(i) / 1000.0;
  217.     rf.put(&r);
  218.   }
  219.   assert(rf.good());
  220.   cout << "odd elements of file in reverse order:\n";
  221.   for (i = 9; i >= 0; i -= 2)
  222.   {
  223.     rf[i].get(&r);
  224.     assert(r.c == i + 'a');
  225.     assert(r.i == i);
  226.     cout << r << "\n";
  227.   }
  228.   assert(rf.good());
  229. #ifdef _OLD_STREAMS
  230.   rf.remove();
  231. #else
  232.   rf.close();
  233.   unlink("recfile");
  234. #endif
  235.   assert(!rf.is_open());
  236. }
  237.  
  238. void t6()
  239. {
  240.   cout << "\nMaking PlotFile pf ...";
  241.   static const char plot_name[] = "plot.out";
  242.   PlotFile pf(plot_name);
  243.   assert(pf.good());
  244.   assert(pf.is_open());
  245.   assert(pf.writable());
  246.   assert(!pf.readable());
  247.   pf.move(10,10);
  248.   pf.label("Test");
  249.   pf.circle(300,300,200);
  250.   pf.line(100, 100, 500, 500);
  251.   assert(pf.good());
  252. #ifdef _OLD_STREAMS
  253.   cout << "(You may delete or attempt to plot " << pf.name() << ")\n";
  254. #else
  255.   cout << "(You may delete or attempt to plot " << plot_name << ")\n";
  256. #endif
  257. }
  258.  
  259. void t7()
  260. {
  261.   char ch;
  262.   char mybuf[1000];
  263. #ifdef _OLD_STREAMS
  264.   cout << "creating string-based ostream...\n";
  265.   ostream strout(1000, mybuf);
  266. #else
  267.   cout << "creating ostrstream...\n";
  268.   ostrstream strout(mybuf, 1000);
  269. #endif
  270.   assert(strout.good());
  271.   assert(strout.writable());
  272.   strout << "This is a string-based stream.\n";
  273.   strout << "With two lines.\n";
  274.   strout.put(char(0));
  275.   assert(strout.good());
  276.   cout << "with contents:\n";
  277.   cout << mybuf;
  278. #ifdef _OLD_STREAMS
  279.   cout << "using it to create string-based istream...\n";
  280.   istream strin(strlen(mybuf), mybuf);
  281. #else
  282.   cout << "using it to create istrstream...\n";
  283.   istrstream strin(mybuf, strlen(mybuf));
  284. #endif
  285.   assert(strin.good());
  286.   assert(strin.readable());
  287.   cout << "with contents:\n";
  288.   while (strin.get(ch)) cout.put(ch);
  289. }
  290.  
  291. void t8()
  292. {
  293. #ifdef _OLD_STREAMS
  294.   cout << "\nThe following file open should generate error message:";
  295.   cout.flush();
  296.   File ef("shouldnotexist", io_readonly, a_useonly);
  297. #else
  298.   ifstream ef("shouldnotexist");
  299. #endif
  300.   assert(!ef.good());
  301.   assert(!ef.is_open());
  302. }
  303.  
  304. void t9()
  305. {
  306.   char ch;
  307.   static char ffile_name[] = "ffile";
  308.   {
  309.       cout << "\nMaking filebuf streams fout and fin...";
  310.       filebuf foutbuf;
  311. #ifdef _OLD_STREAMS
  312.       foutbuf.open(ffile_name, output);
  313. #else
  314.       foutbuf.open(ffile_name, ios::out);
  315. #endif
  316.       ostream fout(&foutbuf);
  317.       assert(fout.good());
  318.       assert(fout.is_open());
  319.       assert(fout.writable());
  320.       assert(!fout.readable());
  321.       fout << "This file has one line testing output streams.\n";
  322. #ifdef _OLD_STREAMS
  323.       fout.close();
  324.       assert(!fout.is_open());
  325. #endif
  326.   }
  327.   filebuf finbuf;
  328. #ifdef _OLD_STREAMS
  329.   finbuf.open(ffile_name, input);
  330. #else
  331.   finbuf.open(ffile_name, ios::in);
  332. #endif
  333.   istream fin(&finbuf);
  334.   assert(fin.good());
  335.   assert(fin.is_open());
  336.   assert(!fin.writable());
  337.   assert(fin.readable());
  338.   cout << "contents of file:\n";
  339.   while(fin >> ch) cout << ch;
  340. #ifndef _OLD_STREAMS
  341.   cout << '\n';
  342. #endif
  343.   fin.close();
  344.   assert(!fin.is_open());
  345. }
  346.  
  347. main()
  348. {
  349.   t1();
  350.   t2();
  351.   t3();
  352.   t4();
  353.   t5();
  354.   t6();
  355.   t7();
  356.   t9();
  357.   t8(); 
  358.  
  359.   cout << "\nFinal names & states:\n";
  360. #ifdef _OLD_STREAMS
  361.   cout << "cin:      " << cin.name()  << "\t" << cin.rdstate() << "\n";
  362.   cout << "cout:     " << cout.name() << "\t" << cout.rdstate() << "\n";
  363.   cout << "cerr:     " << cerr.name() << "\t" << cerr.rdstate() << "\n";
  364. #else
  365.   cout << "cin:      " << "(stdin)"  << "\t" << cin.rdstate() << "\n";
  366.   cout << "cout:     " << "(stdout)" << "\t" << cout.rdstate() << "\n";
  367.   cout << "cerr:     " << "(stderr)" << "\t" << cerr.rdstate() << "\n";
  368. #endif
  369.   cout << "\nend of test.\n";
  370.   return 0;
  371. }
  372.